package com.qm.console.component.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author fangzhipeng
 * @date 2018/1/24
 */
public class FileUtil {
    public static String getXlsPath() {
        return getPath()+getExcelName();
    }
    public static String getPath(){
        return System.getProperty("user.dir")+ File.separator+"fileUpload"+File.separator;
    }
    public static String getExcelName() {
        return System.currentTimeMillis()+".xls";
    }
    public static String save(File file,String fileFileName) throws IOException {

        String filename=System.currentTimeMillis()+"-"+fileFileName;

        String path=System.getProperty("user.dir").replace("bin","fileupload\\");
        InputStream is = new FileInputStream(file);

        OutputStream os = new FileOutputStream(new File(path, filename));
        byte[] buffer = new byte[500];
        int length = 0;


        while(-1 != (length = is.read(buffer, 0, buffer.length)))
        {
            os.write(buffer);
        }
        os.close();
        is.close();

        return filename;
    }
    public static String savePath(File file,String fileFileName) throws IOException{

        String filename=System.currentTimeMillis()+"-"+fileFileName;

        String path=System.getProperty("user.dir").replace("bin","fileupload\\");
        InputStream is = new FileInputStream(file);

        OutputStream os = new FileOutputStream(new File(path, filename));
        byte[] buffer = new byte[500];
        int length = 0;


        while(-1 != (length = is.read(buffer, 0, buffer.length)))
        {
            os.write(buffer);
        }
        os.close();
        is.close();

        return path;
    }
    public static String saveExcel(File file,String fileFileName) throws IOException{

        String filename=System.currentTimeMillis()+"-"+fileFileName;

        String path=System.getProperty("user.dir").replace("bin","fileupload\\");
        InputStream is = new FileInputStream(file);

        OutputStream os = new FileOutputStream(new File(path, filename));
        byte[] buffer = new byte[500];
        int length = 0;


        while(-1 != (length = is.read(buffer, 0, buffer.length)))
        {
            os.write(buffer);
        }
        os.close();
        is.close();

        return filename;
    }
    public static byte[] readFile(InputStream inputStream) {
        try {
            List<Byte>  bytes = new ArrayList<>();
            int b = 0;
            byte[] buf = new byte[1024];
            while ((b = inputStream.read(buf) ) != -1) {
                for (int i = 0; i < b; i++) {
                    bytes.add(buf[i]);
                }
            }
            byte[] newByte = new byte[bytes.size()];
            for (int i = 0; i < bytes.size(); i++) {
                newByte[i] = bytes.get(i);
            }
            return newByte;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
